﻿@model Kooboo.CMS.Web.Areas.Contents.Models.MediaContentGrid
@using Kooboo.CMS.Content.Models
@{
    Layout = "~/Views/Shared/Popup.cshtml";

    ViewBag.Title = "Media library".Localize();

    var routes = ViewContext.RequestContext.AllRouteValues();

    var fileType = ViewContext.RequestContext.GetRequestValue("fileType");

    var fileTypeDic = new Dictionary<string, List<string>>();
    fileTypeDic.Add("image", new List<string> { "jpg", "png", "gif", "jpeg", "bmp" });

    var displayExtension = string.IsNullOrWhiteSpace(fileType) ? new List<string>() : fileTypeDic.ContainsKey(fileType) ? fileTypeDic[fileType.ToLower()] : new List<string>();

    string action = routes["action"].ToString();
    var folderPaths = (ViewContext.RequestContext.GetRequestValue("FolderName") != null ? FolderHelper.SplitFullName(ViewContext.RequestContext.GetRequestValue("FolderName")) : new string[] { }).ToArray();

    var singleChoice = !string.IsNullOrEmpty(ViewContext.RequestContext.GetRequestValue("SingleChoice"));
}
@helper RenderHeader(bool singleChoice)
{
    <thead>
        <tr>
            <th class="checkbox optional-selector ">
                @if (singleChoice == false)
                {
                    <div>
                        <input type="checkbox" class="select-all" />
                        <ul class="hide">
                            <li>Select:</li>
                            <li class="all"><a>
                                @Html.Raw("All Elements".Localize())</a></li>
                            <li class="docs"><a>
                                @Html.Raw("Only Documents".Localize())</a></li>
                            <li class="folders"><a>
                                @Html.Raw("Only Folders".Localize())</a></li>
                            <li class="none"><a>
                                @Html.Raw("None".Localize())</a></li>
                        </ul>
                    </div>
                }
            </th>
            <th>
                @Html.Raw("Title".Localize())
            </th>
            <th class="type">
                @Html.Raw("Type".Localize())
            </th>
            <th class="date">
                @Html.Raw("Date".Localize())
            </th>
            <th class="size">
                @Html.Raw("Size".Localize())
            </th>
        </tr>
    </thead>
}
<div id="main-container">
    <div class="wrap">
        <div class="topbar clearfix">
            <h1 class="title">
                @ViewBag.Title <strong>@ViewContext.RequestContext.GetRequestValue("folderPath")</strong></h1>

            @if (folderPaths.Length > 0)
            {
                <div class="breadcrumb">
                    <a href="@Url.Action(action, ViewContext.RequestContext.AllRouteValues().Merge("FolderName", ""))">@("Root".Localize())</a>

                    @for (var i = 0; i < folderPaths.Length; i++)
                    {
                        if (i == folderPaths.Length - 1)
                        {
                        @Html.IconImage("arrow gray-right")
                        <span>@folderPaths[i]</span>
                        }
                        else
                        {
                        @Html.IconImage("arrow gray-right")
                        <a href="@Url.Action(action, ViewContext.RequestContext.AllRouteValues().Merge("FolderName", FolderHelper.CombineFullName(folderPaths.Take(i + 1))))">@folderPaths[i]</a>
                        }
                    }
                </div>
            }

            @(Html.Partial("_Search"))
            <ul class="view-switch">
                <li class="list active"><a title="@("Details view".Localize())" href="@Url.Action(ViewContext.RequestContext.GetRequestValue("action"), ViewContext.RequestContext.GetRequestValue("controller"), ViewContext.RequestContext.AllRouteValues().Merge("listType", null))">@Html.IconImage("list")</a></li>
                <li class="grid"><a title="@("Thumbnails view".Localize())" href="@Url.Action(ViewContext.RequestContext.GetRequestValue("action"), ViewContext.RequestContext.GetRequestValue("controller"), ViewContext.RequestContext.AllRouteValues().Merge("listType", "Grid"))">@Html.IconImage("grid")</a></li>
            </ul>
        </div>

        <div class="common-table fixed">
            <div class="thead">
                <table>
                    @RenderHeader(singleChoice)
                </table>
            </div>
            <div class="tbody">
                <table>
                    @RenderHeader(singleChoice)
                    <tbody>
                        <tr id="CreateFolderTemplate" class="hide">
                            <td></td>
                            <td>
                                @using (Html.BeginForm(ViewContext.RequestContext.AllRouteValues().Merge("action", "CreateFolder").Merge("UUID", null).Merge("return", ViewContext.HttpContext.Request.RawUrl)))
                                {
                                    @Html.Hidden("FolderName", ViewContext.RequestContext.GetRequestValue("FolderName"))
                                    <input class="editor" type="text" name="Name"
                                @(new[] {Validator.Regex(RegexPatterns.FileName,"Invalid folder name.".Localize()).ClientValidationRule,
                                Validator.Required().ClientValidationRule, 
                                ValidatorExtensions.Remote(Url.Action("IsFolderNameAvailable", ViewContext.RequestContext.AllRouteValues())) }.GenerateHtmlAttributes())/>
                                    <a class="action" data-ajaxform="">@Html.IconImage("save small")</a>
                                    <a class="action" data-inline-action='{"hide":"#CreateFolderTemplate"}'>@Html.IconImage("minus small")</a>
                                    @Html.ValidationMessageForInput("Name")
                                }
                            </td>
                            <td></td>
                            <td class="date"></td>
                            <td></td>
                        </tr>

                        @if (Model.ChildFolders.Count() == 0 && (Model.Contents == null || Model.Contents.Count() == 0))
                        { 
                            <tr class="empty">
                                <td colspan="6">
                                    @("Empty".Localize())
                                </td>
                            </tr>
                        }
                        @foreach (var folder in Model.ChildFolders)
                        {
                            var currentRoutes = ViewContext.RequestContext.AllRouteValues();
                            currentRoutes["folderName"] = folder.FullName;
                
                            <tr class="foldertr">
                                <td class="checkbox">
                                    <input type="@(singleChoice ? "radio" : "checkbox")" disabled="disabled" />

                                </td>
                                <td>
                                    <a href="@this.Url.Action(action, currentRoutes)">@Html.IconImage("folder")@folder.Name</a>
                                </td>
                                <td class="type">
                                    @Html.Raw("Folder".Localize())
                                </td>
                                <td class="date"></td>
                                <td class="size"></td>
                            </tr>
                        }
                        @if (Model != null && Model.Contents != null)
                        {
                            foreach (var content in Model.Contents)
                            {
                                var currentRoutes = ViewContext.RequestContext.AllRouteValues();
                                currentRoutes["folderName"] = ViewContext.RequestContext.GetRequestValue("folderName");
                                currentRoutes["uuid"] = content.UUID;

                                if (displayExtension.Count == 0 || displayExtension.Contains(content.FileType.ToLower()))
                                {
                            <tr class="doctr">
                                <td class="checkbox">
                                    <input  type="@(singleChoice ? "radio" : "checkbox")" name="select" value="@Html.Raw(content.UUID)" class="select doc" 
                            data-alt-text="@Html.Raw((content.Metadata == null || string.IsNullOrEmpty(content.Metadata.AlternateText)) ? content.FileName : content.Metadata.AlternateText)" 
                            data-is-image="@content.IsImage"
                            data-url="@Html.Raw(content.Url)" autocomplete="off"/>
                                </td>
                                <td><a>@Html.IconImage("file " + (content.IsImage ? "image " : "") + content.FileType.ToLower())@content.FileName</a></td>
                                <td class="type">
                                    @Html.Raw(content.FileType.Localize().ToUpper())
                                </td>
                                <td class="date">
                                    @Html.Raw(content.UtcCreationDate.ToShortDateString())
                                </td>
                                <td class="size">
                                    @Html.Raw(string.Format("{0:0.00}", content.Size / 1024))
                                    kb
                                </td>
                            </tr>
                                }
                            }
                        }
                    </tbody>
                </table>
            </div>
            <div class="pagination">
                @(Html.Pager(Model.Contents, "", ViewContext.RequestContext.AllRouteValues(), null))
            </div>
        </div>
    </div>
</div>
<div class="buttons">
    <a id="CreateFolder" class="button action left">@("Create folder".Localize())</a>
    @if (folderPaths.Length > 0)
    {
        <a class="upload-button button upload left">
            <span>
                <form action="@this.Url.Action("Upload", ViewContext.RequestContext.AllRouteValues().Merge("return", ViewContext.HttpContext.Request.RawUrl))" enctype="multipart/form-data" method="post"  class="no-stop">
                    <input type="hidden" value="True" name="Overrided" />
                    <input type="file" name="file" title="@("Upload".Localize())" />
                </form>
            </span>
            @("Upload".Localize())
        </a>
    }
    <button id="addBtn">
        @("Insert".Localize())</button>

    <a id="cancelBtn" class="button gray cancel action">@("Cancel".Localize())</a>
</div>
<script type="text/javascript">
    $(function () {
        var $table = $('.common-table table');
        $table.checkableTable();
        $table.mixedGrid();
        $table.gridInlineEdit();

        $('#CreateFolder').click(function () {
            $('#CreateFolderTemplate').removeClass('hide');
            $('.empty').hide();
        });
    });
</script>
@Html.Partial("Selection_Script")